package conf2.ui;

import conf2.model.EActor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import static conf2.model.EActor.*;

public class Portraits {

    private static Map<EActor, BufferedImage> images = new HashMap<EActor, BufferedImage>();
    private static Map<EActor, Icon> icons = new HashMap<EActor, Icon>();
    private static BufferedImage defaultPortrait = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    private static BufferedImage variantStar = null;


    static {
        Graphics g = defaultPortrait.getGraphics();
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, 100, 100);
        g.setColor(Color.BLACK);
        g.drawString("No image", 10, 40);
    }

    public static void preload() {

        try {
            variantStar = ImageIO.read(Portraits.class.getResourceAsStream("/conf2/images/variant_star.gif"));
        } catch (Exception e) {
            Logger.getLogger(Portraits.class.getName()).log(
                    Level.SEVERE, "Could not read /conf2/images/variant_star.gif!");
        }

        images.put(C_FRODO, load("/conf2/images/frodo.jpg"));
        images.put(C_SAM, load("/conf2/images/sam.jpg"));
        images.put(C_MERRY, load("/conf2/images/merry.jpg"));
        images.put(C_PIPPIN, load("/conf2/images/pippin.jpg"));
        images.put(C_ARAGORN, load("/conf2/images/aragorn.jpg"));
        images.put(C_GANDALF, load("/conf2/images/gandalf.jpg"));
        images.put(C_LEGOLAS, load("/conf2/images/legolas.jpg"));
        images.put(C_GIMLI, load("/conf2/images/gimli.jpg"));
        images.put(C_BOROMIR, load("/conf2/images/boromir.jpg"));

        images.put(V_FRODO, load("/conf2/images/v_frodo.jpg"));
        images.put(V_SAM, load("/conf2/images/v_sam.jpg"));
        images.put(V_ELROND, load("/conf2/images/v_elrond.jpg"));
        images.put(V_FARAMIR, load("/conf2/images/v_faramir.jpg"));
        images.put(V_ARAGORN, load("/conf2/images/v_aragorn.jpg"));
        images.put(V_GANDALF, load("/conf2/images/v_gandalf.jpg"));
        images.put(V_THEODEN, load("/conf2/images/v_theoden.jpg"));
        images.put(V_SMEAGOL, load("/conf2/images/v_smeagol.jpg"));
        images.put(V_TREEBEARD, load("/conf2/images/v_treebeard.jpg"));

        images.put(C_CAVETROLL, load("/conf2/images/cavetroll.jpg"));
        images.put(C_WARG, load("/conf2/images/warg.jpg"));
        images.put(C_WITCHKING, load("/conf2/images/witchking.jpg"));
        images.put(C_SHELOB, load("/conf2/images/shelob.jpg"));
        images.put(C_SARUMAN, load("/conf2/images/saruman.jpg"));
        images.put(C_ORCS, load("/conf2/images/orcs.jpg"));
        images.put(C_BALROG, load("/conf2/images/balrog.jpg"));
        images.put(C_B_RIDERS, load("/conf2/images/riders.jpg"));
        images.put(C_F_NAZGUL, load("/conf2/images/nazgul.jpg"));

        images.put(V_F_NAZGUL, load("/conf2/images/v_nazgul.jpg"));
        images.put(V_GOLLUM, load("/conf2/images/v_gollum.jpg"));
        images.put(V_MOUTHOFSAURON, load("/conf2/images/v_mouthofsauron.jpg"));
        images.put(V_ORCS, load("/conf2/images/v_orcs.jpg"));
        images.put(V_SARUMAN, load("/conf2/images/v_saruman.jpg"));
        images.put(V_URUKHAI, load("/conf2/images/v_urukhai.jpg"));
        images.put(V_WATCHER, load("/conf2/images/v_watcher.jpg"));
        images.put(V_WITCHKING, load("/conf2/images/v_witchking.jpg"));
        images.put(V_WORMTONGUE, load("/conf2/images/v_wormtongue.jpg"));

        for (EActor ea : images.keySet()) {
            Image img = get(ea).getScaledInstance(50, 50, Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(img);
            icons.put(ea, icon);
        }
    }

    public static BufferedImage getDefault() {
        return defaultPortrait;
    }

    public static Icon randomIcon(Side mySide) {
        EActor[] actors = (mySide == Side.LIGHT ? EActor.LIGHT_ACTORS : EActor.DARK_ACTORS);
        int i = (int)(Math.random()*actors.length);
        return new ImageIcon(get(actors[i]));
    }

    private static BufferedImage load(String path) {
        try {
            BufferedImage img = ImageIO.read(Portraits.class.getResourceAsStream(path));
            Graphics g = img.getGraphics();
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
            return img;
        } catch (Exception e) {
            Logger.getLogger(Portraits.class.getName()).log(
                    Level.SEVERE, "Could not read portrait image " + path);
            return defaultPortrait;
        }
    }

    public static BufferedImage get(EActor who) {
        BufferedImage img = images.get(who);
        if (who.isVariant()) {
            Graphics g = img.getGraphics();
            g.drawImage(variantStar, img.getWidth() - variantStar.getWidth() - 2,
                    2, null);
        }

        return img;
    }

    public static Icon getIcon(EActor who) {
        return icons.get(who);
    }

    public static BufferedImage getNumberedPortrait(
            EActor which, int number) {
        BufferedImage portrait = get(which);

        BufferedImage numberedPortrait = new BufferedImage(portrait.getWidth(null),
                portrait.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) numberedPortrait.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawImage(portrait, 0, 0, null);

        g.setFont(FontSet.getPortraitAttackValue());
        String str = String.valueOf(number);
        int strw = g.getFontMetrics().stringWidth(str);

        int x = numberedPortrait.getWidth(null) - strw - 3;
        int y = numberedPortrait.getHeight(null) - 3;
        g.setColor(Color.BLACK);
        g.drawString(str, x + 1, y + 1);
        g.setColor(Color.WHITE);
        g.drawString(str, x, y);

        return numberedPortrait;
    }

    public static BufferedImage getLabeledNumberedPortrait(
            EActor which, String label, int number) {
        BufferedImage numbered = getNumberedPortrait(which, number);

        BufferedImage np = new BufferedImage(numbered.getWidth(null),
                numbered.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g = (Graphics2D) np.getGraphics();
        g.drawImage(numbered, 0, 0, null);
        g.setFont(FontSet.getPortraitLabel());
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        int strw = g.getFontMetrics().stringWidth(label);
        int strh = g.getFontMetrics().getHeight();
        int x = (np.getWidth() - strw) / 2;
        int y = strh;
        g.setColor(Color.BLACK);
        g.drawString(label, x + 1, y + 1);
        g.setColor(Color.WHITE);
        g.drawString(label, x, y);

        return np;
    }

    public static BufferedImage getLabeledNumberedNamedPortrait(
            EActor actor, String label, int number, String name) {
        BufferedImage img = getLabeledNumberedPortrait(actor, label, number);
        Graphics2D g = (Graphics2D) img.getGraphics();

        g.setFont(FontSet.getPortraitLabel());
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        int y = img.getHeight() - 5;
        int x = 3;

        g.setColor(Color.BLACK);
        g.drawString(name, x + 1, y + 1);
        g.setColor(Color.WHITE);
        g.drawString(name, x, y);

        return img;
    }
}
